//
//  RootViewController.m
//  decision
//
//  Created by Zhiguo on 03.02.10.
//  Copyright Home 2010. All rights reserved.
//

#import "RootViewController.h"
#import "decisionPoint.h"
#import "decisionAppDelegate.h"
#import "dpCell.h"


@implementation RootViewController
@synthesize decisionPointInputVar;

- (void)viewDidLoad {
    [super viewDidLoad];

	
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem: )];
	self.navigationItem.leftBarButtonItem = addButton;
	[addButton release];
	
}

- (void)addItem:sender { 
	//NSLog(@"OK, I am in addItem now!");
	if(self.decisionPointInputVar == nil){
		decisionPointInput *tempdecisionPointInputVar = [[decisionPointInput alloc] initWithNibName:@"decisionPointInput" bundle:[NSBundle mainBundle]];
		self.decisionPointInputVar = tempdecisionPointInputVar;
		[tempdecisionPointInputVar release];
	}
	
	[self.navigationController pushViewController:self.decisionPointInputVar animated:NO];	
	
	self.decisionPointInputVar.title = @"A new decision";

	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cacelItem:)];
	self.decisionPointInputVar.navigationItem.leftBarButtonItem = backButton;
	[backButton release];
	
	UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneItem:)];
	self.decisionPointInputVar.navigationItem.rightBarButtonItem = doneButton;
	[doneButton release];
	return;	
}

-(void)cacelItem:sender {
	//NSLog(@"OK, I am in cacelItem now!");
	[self.navigationController popViewControllerAnimated:YES];
	return;
}

-(void)doneItem:sender{

	
	decisionPoint *newDp = [[decisionPoint alloc] initWithTandD:self.decisionPointInputVar.truncateTextField.text desc:self.decisionPointInputVar.descriptionTextView.text];
	NSInteger saveResult =  [newDp insertNewDpIntoDatabase];
	NSLog(@"saveResult is %d", saveResult);
	if(saveResult){
		
		[self.navigationController popViewControllerAnimated:YES];
		decisionAppDelegate *appDelegate = (decisionAppDelegate *)[[UIApplication sharedApplication] delegate];
		[appDelegate.decisionPoints addObject:newDp];
		[newDp release];
	
		NSArray *insertIndexPaths = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:(saveResult-1) inSection:0], nil];
	
		[self.tableView beginUpdates];
		[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];
		[self.tableView endUpdates];
	}
	

	
	return;
}
/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/

- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}


/*
 // Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations.
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	decisionAppDelegate *appDelegate = (decisionAppDelegate *)[[UIApplication sharedApplication] delegate];
	//NSLog(@"appDelegate.decisionPoints.count: %d",appDelegate.decisionPoints.count);
    return appDelegate.decisionPoints.count;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    dpCell *cell = (dpCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		//NSLog(@"cell is nil");
        cell = [[[dpCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    decisionAppDelegate *appDelegate = (decisionAppDelegate *)[[UIApplication sharedApplication] delegate];
	decisionPoint *dp = [appDelegate.decisionPoints objectAtIndex:indexPath.row];
	[cell setdp:dp];
	//NSLog(@"cell text: %@", dp.truncate);
	// Configure the cell.

    return cell;
}



/*
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Navigation logic may go here -- for example, create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController animated:YES];
	// [anotherViewController release];
}
*/


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

